package com.wwt.springboot_vue.util;

import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

//跨域拦截
@Component
public class webFilter extends CorsFilter {
    //构造方法初始化
    public webFilter(){
        super(configurationSource());
    }

    //编写过滤url的配置方法
    private static UrlBasedCorsConfigurationSource configurationSource(){
        CorsConfiguration configuration=new CorsConfiguration();
        configuration.setAllowCredentials(true);
        configuration.addAllowedHeader("*");
        configuration.addAllowedOrigin("*");
        configuration.setMaxAge(36000L);
        //允许的方式
        configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","DELETE","OPTIONS"));

        UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
//        允许的路径
        source.registerCorsConfiguration("/**",configuration);
        return source;
    }
}
